package com.woniu.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.entity.TbJpRole;
import com.woniu.resultSet.Result;
import com.woniu.web.service.TbJpPermissionService;
import com.woniu.web.service.TbJpRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.SchemaOutputResolver;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sxs
 * 2020/9/24
 * 角色模块操作
 * */
@RestController
@RequestMapping("/role")
@Api(value = "角色相关")
public class TbJpRoleControoller {
    @Resource
    TbJpRoleService roleService;
    @Resource
    TbJpPermissionService permissionService;
    @GetMapping("/")
    @ApiOperation(value = "查看全部角色", notes = "This method for 查找所有角色")
    public Result findAllRoles(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="5") Integer pageSize,String roleName){
        Result result=new Result();
        System.out.println(pageNum+"=========="+pageSize);
        try {
            Map map =new HashMap();
            PageHelper.startPage(pageNum,pageSize);
            List<TbJpRole> allRoles = roleService.findAllRoles(roleName);
            PageInfo<TbJpRole> pageInfo=new PageInfo<>(allRoles);

            result.setObj(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/")
    @ApiOperation(value = "增加角色", notes = "This method for 查找所有角色,增加角色")
    public Result addRole(@RequestBody TbJpRole tbJpRole){
        Result result=new Result();
        try {
           
             roleService.addRole(tbJpRole);
             result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除角色", notes = "This method for 查找所有角色,删除角色")
    public Result delRole(@PathVariable Integer roleId){
        Result result=new Result();
        try {
            roleService.delRole(roleId);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/delMore")
    @ApiOperation(value = "批量删除角色", notes = "This method for 查找所有角色,批量删除角色")
    public Result delRoleMore(@RequestBody Integer[] roleIds){
        Result result=new Result();
        try {
            roleService.delRoleMore(roleIds);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PutMapping("/")
    @ApiOperation(value = "修改角色", notes = "This method for 查找所有角色,修改角色")
    public Result updateRole(@RequestBody TbJpRole tbJpRole){
        Result result=new Result();
        try {
            roleService.updateRole(tbJpRole);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("/rolePermission")
    @ApiOperation(value = "查看指定角色权限", notes = "This method for 查看指定角色权限")
    public Result findAllRolesPermission( Integer roleId){

        Result result=new Result();

        try {
            Map<String, Object> allPermissionTree = permissionService.findAllPermissionTree(roleId);
            result.setObj(allPermissionTree);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/rolePermission")
    @ApiOperation(value = "增加角色权限", notes = "This method for 查找所有角色,增加角色权限")
    public Result addRolePermission(@RequestBody Integer[] permissionIds){


        Result result=new Result();
        try {

            permissionService.addRolePermission(permissionIds);
            result.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
